package com.tjec.project.projectLeaderWorkbeanch.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import com.tjec.common.constant.AttachmentConstants;
import com.tjec.common.constant.Constants;
import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.SecurityUtils;
import com.tjec.common.utils.ServletUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.companyLibrary.domain.BmsExpertCompany;
import com.tjec.project.portal.service.IBmsMsgService;
import com.tjec.project.system.domain.BmsAttachment;
import com.tjec.project.system.domain.BmsExpertCertification;
import com.tjec.project.system.domain.SysRole;
import com.tjec.project.system.domain.SysUser;
import com.tjec.project.system.mapper.BmsAttachmentMapper;
import com.tjec.project.system.mapper.BmsExpertCertificationMapper;
import com.tjec.project.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.projectLeaderWorkbeanch.mapper.BmsExpertPersonMapper;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsExpertPerson;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsExpertPersonService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 个人专家Service业务层处理
 *
 * @author tjec
 * @date 2020-04-20
 */
@Service
public class BmsExpertPersonServiceImpl implements IBmsExpertPersonService
{
    @Autowired
    private BmsExpertPersonMapper bmsExpertPersonMapper;

    @Autowired
    private BmsExpertCertificationMapper bmsExpertCertificationMapper;

    @Autowired
    private BmsAttachmentMapper bmsAttachmentMapper;

    @Autowired
    private IBmsMsgService bmsMsgService;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询个人专家
     *
     * @param guid 个人专家ID
     * @return 个人专家
     */
    @Override
    public BmsExpertPerson selectBmsExpertPersonByGuid(String guid)
    {
        return bmsExpertPersonMapper.selectBmsExpertPersonByGuid(guid);
    }

    /**
     * 查询个人专家列表
     *
     * @param bmsExpertPerson 个人专家
     * @return 个人专家
     */
    @Override
    public List<BmsExpertPerson> selectBmsExpertPersonList(BmsExpertPerson bmsExpertPerson)
    {
        return bmsExpertPersonMapper.selectBmsExpertPersonList(bmsExpertPerson);
    }

    /**
     * 新增个人专家
     *
     * @param bmsExpertPerson 个人专家
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBmsExpertPerson(BmsExpertPerson bmsExpertPerson)
    {
        String expertId = UUID.randomUUID().toString();
        bmsExpertPerson.setCreateTime(DateUtils.getNowDate());
        bmsExpertPerson.setCreateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        if(null==bmsExpertPerson.getGuid()||("").equals(bmsExpertPerson.getGuid())){
            bmsExpertPerson.setGuid(expertId);
        }
        parseBmsExpertPerson(bmsExpertPerson);
        return bmsExpertPersonMapper.insertBmsExpertPerson(bmsExpertPerson);
    }

    /**
     * 解析个人专家信息
     * @param bmsExpertPerson
     */
    private void parseBmsExpertPerson(BmsExpertPerson bmsExpertPerson){
        //专业分类
        if(null!=bmsExpertPerson.getProfessionCategorys()&&bmsExpertPerson.getProfessionCategorys().length>0){
            bmsExpertPerson.setProfessionCategory(StringUtils.arrayToString(bmsExpertPerson.getProfessionCategorys()));
        }
        //行业类别
        if(null!=bmsExpertPerson.getIndustryMerits()&&bmsExpertPerson.getIndustryMerits().length>0){
            bmsExpertPerson.setIndustryMerit(StringUtils.arrayToString(bmsExpertPerson.getIndustryMerits()));
        }
        //业务分类
        if(null!=bmsExpertPerson.getBusinessMerits()&&bmsExpertPerson.getBusinessMerits().length>0){
            bmsExpertPerson.setBusinessMerit(StringUtils.arrayToString(bmsExpertPerson.getBusinessMerits()));
        }
        //出生年月日转换成年龄
        if(null!=bmsExpertPerson.getBirthday()){
            bmsExpertPerson.setAge(Long.valueOf(DateUtils.yearCompare(bmsExpertPerson.getBirthday(),DateUtils.getNowDate())));
        }
        ///更新专家持证情况、上传附件，每次删除原来的，添加最新的
        if(StringUtils.isNotBlank(bmsExpertPerson.getGuid())){
            bmsExpertCertificationMapper.deleteBmsExpertCertificationByExpertId(bmsExpertPerson.getGuid());
            bmsAttachmentMapper.updateBmsAttachmentByBizId(new String[]{bmsExpertPerson.getGuid()}, tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        }
        if(!CollectionUtils.isEmpty(bmsExpertPerson.getBmsExpertCertifications())){
            for (BmsExpertCertification bmsExpertCertification : bmsExpertPerson.getBmsExpertCertifications()) {
                bmsExpertCertification.setGuid(UUID.randomUUID().toString());
                bmsExpertCertification.setExpertId(bmsExpertPerson.getGuid());
                bmsExpertCertificationMapper.insertBmsExpertCertification(bmsExpertCertification);
            }
        }
        if(!CollectionUtils.isEmpty(bmsExpertPerson.getAttachmentIds())){
            bmsAttachmentMapper.updateBmsAttachmentByGuidsUpdateBiz(bmsExpertPerson.getAttachmentIds(), bmsExpertPerson.getGuid(), AttachmentConstants.EXPERT_PERSON, tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId());
        }
       /*if(!CollectionUtils.isEmpty(bmsExpertPerson.getBmsAttachments())){
            ArrayList<String> attGuids = new ArrayList<String>();
            for (BmsAttachment bmsAttachment : bmsExpertPerson.getBmsAttachments()) {
                if(StringUtils.isBlank(bmsAttachment.getBizId())){
                    attGuids.add(bmsAttachment.getGuid());
                }else {
                    BmsAttachment att = new BmsAttachment();
                    att.setGuid(bmsAttachment.getGuid());
                    att.setDelFlag("0");
                    bmsAttachmentMapper.updateBmsAttachment(att);
                }
            }
            if(!CollectionUtils.isEmpty(attGuids)){
                bmsAttachmentMapper.updateBmsAttachmentByGuidsUpdateBiz(attGuids, bmsExpertPerson.getGuid(), AttachmentConstants.EXPERT_PERSON);
            }
        }*/
    }

    /**
     * 修改个人专家
     *
     * @param bmsExpertPerson 个人专家
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBmsExpertPerson(BmsExpertPerson bmsExpertPerson)
    {
        bmsExpertPerson.setUpdateTime(DateUtils.getNowDate());
        bmsExpertPerson.setUpdateBy(SecurityUtils.getLoginUser().getUser().getUserId());
        parseBmsExpertPerson(bmsExpertPerson);
        if(bmsExpertPerson.getRecommendStatus()!=null&&bmsExpertPerson.getRecommendStatus().equals("1")){
            pushMsg(bmsExpertPerson);
        }
        return bmsExpertPersonMapper.updateBmsExpertPerson(bmsExpertPerson);
    }

    /**
     * 人专家库修改信息，则通知企业专家库管理员“XXX专家信息有更新，请核实。”
     */
    private void pushMsg(BmsExpertPerson bmsExpertPerson) {
        Set<String> userIds = sysUserRoleMapper.selectUserIdByRoleKey(Constants.ROLE_ENTERPRISE_EXPERT_LIBRARY_ADMIN_KEY);
        if(!CollectionUtils.isEmpty(userIds)){
            for (String userId : userIds) {
                String msgType = "推荐至企业专家库";
                String content = bmsExpertPersonMapper.selectBmsExpertPersonByGuid(bmsExpertPerson.getGuid()).getName()+"专家信息有更新，请核实。";
                bmsMsgService.insertByReceiverUser(userId, msgType, content,true);
            }
        }
    }

    /**
     * 批量删除个人专家
     *
     * @param ids 需要删除的个人专家ID
     * @return 结果
     */
    @Override
    public int deleteBmsExpertPersonByIds(String[] ids)
    {
        return bmsExpertPersonMapper.deleteBmsExpertPersonByIds(ids);
    }

    /**
     * 删除个人专家信息
     *
     * @param id 个人专家ID
     * @return 结果
     */
    @Override
    public int deleteBmsExpertPersonById(String id)
    {
        return bmsExpertPersonMapper.deleteBmsExpertPersonById(id);
    }
}
